#!/bin/env  python
# -*- coding: gbk -*- 
import sys
import time
import datetime

path = "/home/spider/atscript/lib/"
sys.path.append(path)

from atscript import *
from hadoop import *
from assert_func import *

conf = config("rankget.yaml")
sample = base(conf.get("CONF"), conf.get("SCHEDULE"), conf.get("JOBLIST"))
#hadoop = hadoop(conf, "/home/spider/rankget/conf/hadoop-site.xml")
hadoop_raw = hadoop(conf)
hadoop = hadoop(conf, "/home/spider/rankget/conf/hadoop-site.xml")

class Test_rg_master_clickprepare(at_case):
    def __init__(self, casename, base, jobname, input, output):
        at_case.__init__(self, casename, base, jobname, input, output)
        
    def set_input(self):
        line1 = ".multidomain.com\tMULTIDOMAIN"
        line2 = "www.notmultidomain.com\tNOTMULTIDOMAIN"
        hadoop_raw.rmdir("/user/spider/rankget/output/site_to_train/")
        hadoop_raw.mkdir("/user/spider/rankget/output/site_to_train/")
        input_content = "\n".join([line1, line2])
        temp_input_file = self.casename + ".input"
        try:
            file_handle = open(temp_input_file, 'w')
            file_handle.write(input_content)
            file_handle.write("\n")
        except IOErro:
            raise IOError
        finally:
            file_handle.close()
        hadoop_raw.put(temp_input_file, "/user/spider/rankget/output/site_to_train/")

    def run(self):
        print self.run_cmd
        self.execute_cmd(self.run_cmd, True)

    def check_result(self):
        # 1. ¼ì²é±¾µØÄ¿Â¼ºÍHDFSÄ¿Â¼ÊÇ·ñ´´½¨³É¹¦
        result_flag = True 
        if not expect_exsits(conf.get("MODULE_HOME"), conf.get("MODULE_HOME")+"bin", conf.get("MODULE_HOME")+"tmp") :
            result_falg = False
              
        if not( hadoop.test("/user/spider/rankget/") and hadoop.test("/user/spider/rankget/bin") and hadoop.test("/user/spider/rankget/input") ):
            result_flag = False

        # 2. ¼ì²éÊÇ·ñÕýÈ·ÉÏ´«rankget.zipÎÄ¼þ, ¼ì²éÊÇ·ñÕýÈ·ÉÏ´«select_name_onlyÎÄ¼þ, ÊÇ·ñÕýÈ·ÉÏ´«site.maualºÍwhite.manual
        if not( hadoop.test("/user/spider/rankget/bin/rankget.zip") and hadoop.test("/user/spider/rankget/input/downloadclick.list") and hadoop.test("/user/spider/rankget/input/site.manual") and hadoop.test("/user/spider/rankget/input/white.manual")):
            result_flag = False

        # 3. ¼ì²é»ñÈ¡µÄÕ¾µãÁÐ±íÊÇ·ñÕýÈ·
        expect_out1 = self.casename + ".notmultidomain.expect_out"
        file_handle = open(expect_out1, 'w')
        file_handle.write("www.notmultidomain.com\n")
        file_handle.close()

        expect_out2 = self.casename + ".multidomain.expect_out"
        file_handle = open(expect_out2, 'w')
        file_handle.write(".multidomain.com\n")
        file_handle.close()
 
        try:
            assert_file_equal(expect_out1, conf.get("MODULE_HOME")+"data/sample-site.list.notmultidomain")        
            assert_file_equal(expect_out2, conf.get("MODULE_HOME")+"data/sample-site.list.multidomain")        
        except AssertionError, e:
            print >> sys.stderr, conf.get("MODULE_HOME")+"data/sample-site.list.notmultidomain or sample-site.list.multidomain content error!"
            result_flag = False

        # 4. ¼ì²éµã»÷Õ¹ÏÖÁÐ±íÊÇ·ñ»ñÈ¡ÕýÈ· 
        expect_out = self.casename +  ".downloadclick.expect_out"
        real_out = self.casename + ".downloadclick.real_out"
         
        # 4.1. ÆÚÍûµÄ²ú³ö
        today = time.strftime('%Y%m%d',time.localtime(time.time())) 
        days = getdate(today, 30)
        file_handle = open(expect_out, 'w')
        for i in range(60):
            if i < 30:
                line = "%s ftp://jp01-rs-build0.jp01.baidu.com/home/work/opdir/zwj_bak_for_click/data/%s/clickresult_0.log.%s\n" % (i, days[i], days[i])
                file_handle.write(line)
            if i >= 30:
                #  ftp://jp01-rs-build0.jp01.baidu.com/home/logjob/ui_urllog/data/20110703/url.log
                line = "%s ftp://jp01-rs-build0.jp01.baidu.com/home/logjob/ui_urllog/data/%s/url.log\n" %(i, days[i-30])
                file_handle.write(line)
        file_handle.close() 

        # 4.2. Êµ¼ÊµÄ²ú³ö
        self.execute_cmd("rm -f %s" % real_out)
        hadoop.get(self.input+"downloadclick.list", real_out) 

        # 4.3 ±È½ÏÆÚÍû²ú³öÓëÊµ¼Ê²ú³ö 
	try:
            assert_file_equal(expect_out, real_out)
        except AssertionError, e:
            print >>sys.stout, "get click list error!"
            result_flag = False

        if result_flag:
            print >>sys.stdout, "case %s passed" % self.casename
        else :
            print >>sys.stdout, "case %s failed" % self.casename

def getdate(date, intval):
    year, mon, day = int(date[:4]), int(date[4:6]), int(date[6:])
    d = datetime.datetime(year, mon, day)
    days = []
    for i in range(1,intval+1):
        c = d - datetime.timedelta(i)
        days.append(c.strftime('%Y%m%d'))
    return days

if __name__ == "__main__":
     test = Test_rg_master_clickprepare('Test_rg_master_clickprepare', sample, 'rg_master_clickprepare', "/user/spider/rankget/input/", "")
     test.set_input()
     test.run()
     test.check_result()  
